package com.wrupple.muba.catalogs.client.widgets.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.cobogw.gwt.user.client.CSS;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockLayoutPanel.Direction;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.wrupple.muba.catalogs.client.events.DataSelectionRequest;
import com.wrupple.muba.catalogs.client.view.GenericSelectionToolbar;
import com.wrupple.muba.catalogs.domain.CatalogEntry;
import com.wrupple.muba.common.shared.StateTransition;
import com.wrupple.muba.widget.client.widgets.panels.PanelWithToolbar;
import com.wrupple.muba.widget.client.widgets.toolbar.ActionToolbar;

public class GenericSelectionToolbarImpl extends ResizeComposite implements GenericSelectionToolbar {

	private ImageResource genericIcon;
	private PanelWithToolbar main;
	private VerticalPanel panel;
	private StateTransition<List<CatalogEntry>> callback;
	private List<CatalogEntry> selected;

	public GenericSelectionToolbarImpl(CatalogTheme theme) {
		this.genericIcon = theme.empty();
		main = new PanelWithToolbar();
		selected = new ArrayList<CatalogEntry>();
		ScrollPanel container = new ScrollPanel();
		this.panel = new VerticalPanel();
		container.setWidget(panel);
		panel.setWidth("100%");
		main.getContentContainer().setWidget(container);
		Collection<? extends Widget> actions = getActions(theme);
		ActionToolbar actionToolbar = new ActionToolbar();
		actionToolbar.addActions(actions);
		main.addToolbar(actionToolbar);
		initWidget(main);
	}
	@Override
	public void clear() {
		panel.clear();
	}

	
	@Override
	public void addToSelection(final CatalogEntry result) {
		selected.add(result);
		Image image = new Image(genericIcon);
		Label label = new Label(result.getName());
		final Grid grid = new Grid(2,1);
		CSS.setProperty(grid, "marginLeft", "auto");
		CSS.setProperty(grid, "marginRight", "auto");
		CSS.setProperty(grid, "textAlign", "center");
		grid.setWidget(0, 0, image);
		grid.setWidget(1, 0, label);
		
		panel.add(grid);
		
		image.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				panel.remove(grid);
				selected.remove(result);
			}
		});
	}

	
	@Override
	public void start(List<CatalogEntry> parameter,
			StateTransition<List<CatalogEntry>> onDone, EventBus bus) {
		if (parameter == null) {
			parameter = Collections.emptyList();
		}
		selected.clear();
		for (CatalogEntry entry : parameter) {
			addToSelection(entry);
		}
		this.callback = onDone;		
	}
	@Override
	public Direction getDirection() {
		return Direction.LINE_END;
	}

	@Override
	public int getSize() {
		return 250;
	}

	@Override
	public HandlerRegistration addResizeHandler(ResizeHandler handler) {
		return null;
	}

	@Override
	public HandlerRegistration addSelectionRequestHandler(
			com.wrupple.muba.catalogs.client.view.SelectionToolbar.SelectionRequestHandler handler) {
		return addHandler(handler, DataSelectionRequest.TYPE);
	}
	

	private Collection<? extends Widget> getActions(CatalogTheme theme) {
		CatalogUserInterfaceMessages msg = GWT.create(CatalogUserInterfaceMessages.class);
		Button ok = new Button(msg.ok(), new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				
				if(callback!=null){
					List<CatalogEntry> regreso = Collections.unmodifiableList(selected);
					callback.setResultAndFinish(regreso);
					callback = null;
				}
				
			}
		});

		Button cancel = new Button(msg.cancel(), new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				callback.setResultAndFinish(null);
			}
		});

		Button selectall = new Button(msg.selectAll(), new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				DataSelectionRequest request = new DataSelectionRequest();
				fireEvent(request);
				CatalogEntry entry;
				for(JavaScriptObject o : request.getVisibleData()){
					 entry = o.cast();
					addToSelection(entry );
				}
			}
		});
		// TODO select none
		return Arrays.asList(ok, cancel,selectall);
	}


}
